package edu.xhu.adapters;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.bean.JobBean;

import edu.xhu.R;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class EmploymentAdapter extends BaseAdapter{

	private ArrayList<JobBean> jobBeans;
	private LayoutInflater inflater;
	private Context context;
	
	public EmploymentAdapter(Context context, ArrayList<JobBean> jobBeans){
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.jobBeans = jobBeans;
	}
	
	public int getCount() {
		if (null == jobBeans)
			return 0;
		return jobBeans.size();
	}

	public void addItems(ArrayList<JobBean> jobBeans){
		this.jobBeans.addAll(jobBeans);
		notifyDataSetChanged();
	}
	
	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (null == convertView){
			viewHolder 				= new ViewHolder();
			convertView				= inflater.inflate(R.layout.employment_item, null);
			viewHolder.company		= (TextView) convertView.findViewById(R.id.textview_company_name);
			viewHolder.date			= (TextView) convertView.findViewById(R.id.textview_date);
			viewHolder.time			= (TextView) convertView.findViewById(R.id.textview_time);
			viewHolder.place		= (TextView) convertView.findViewById(R.id.textview_place);
			viewHolder.share		= (ImageView) convertView.findViewById(R.id.imageview_share);
			convertView.setTag(viewHolder);
		}else {
			viewHolder				= (ViewHolder) convertView.getTag();
		}
		
		JobBean tmpBean = jobBeans.get(position);
		
		//判断该招聘信息是不是今天,将今天的招聘信息用红色显示
		Date nowTime = new Date();
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		String time1 = format1.format(nowTime);
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-d");
		String time2 = format2.format(nowTime);
		SimpleDateFormat format3 = new SimpleDateFormat("yyyy-M-dd");
		String time3 = format3.format(nowTime);
		SimpleDateFormat format4 = new SimpleDateFormat("yyyy-M-dd");
		String time4 = format4.format(nowTime);
		
		if (tmpBean.date.equals(time1) || tmpBean.date.equals(time2) || tmpBean.date.equals(time3)
				|| tmpBean.date.equals(time4)){
			viewHolder.company.setTextColor(Color.RED);
			viewHolder.date.setTextColor(Color.RED);
			viewHolder.time.setTextColor(Color.RED);
			viewHolder.place.setTextColor(Color.RED);
		}else {
			viewHolder.company.setTextColor(Color.BLACK);
			viewHolder.date.setTextColor(R.color.color_dark_grey);
			viewHolder.time.setTextColor(R.color.color_dark_grey);
			viewHolder.place.setTextColor(R.color.color_dark_grey);
		}
		
		
		viewHolder.company.setText(tmpBean.title);
		viewHolder.date.setText("日期：" + tmpBean.date);
		viewHolder.time.setText("时间：" + tmpBean.time);
		viewHolder.place.setText("地点：" + tmpBean.site);
		
		viewHolder.share.setOnClickListener(new ShareListener(position));
		
		return convertView;
	}
	
	class ViewHolder{
		public TextView company;
		public TextView date;
		public TextView time;
		public TextView place;
		public ImageView share;				//分享
	}
	
	class ShareListener implements OnClickListener{
		private int pos;
		public ShareListener(int pos){
			this.pos = pos;
		}
		@Override
		public void onClick(View v) {
			JobBean tmpBean = jobBeans.get(pos);
			String content = tmpBean.url + "\n"
					+" \"" + tmpBean.title + "\" 会在我们学校招聘。时间："
					+ tmpBean.date + " " + tmpBean.time + "，地点是：" + tmpBean.site + "。"
					+ " 来自掌中西华:http://www.esjiaoyi.com/zzxh/";
			
			Intent intent=new Intent(Intent.ACTION_SEND);
	        intent.setType("text/plain");
	        intent.putExtra(Intent.EXTRA_SUBJECT, "数据的标题");
	        intent.putExtra(Intent.EXTRA_TEXT, content);
	        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	        context.startActivity(Intent.createChooser(intent, "分享"));
		}
	}

}
